package bookstudytest.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

import java.util.Iterator;

/**
 * @Auther: Lee
 * @Date: 2018/6/5 10:57
 * @Description:
 */
public class NioSimple {
    public static void main(String[] args) throws IOException {
//        new NioSimple().createServer();
//        new NioSimple().createHttpServer();
        String a = "Start";
        String b = "start";
        System.out.println(a.equals(b));

    }


    public void createHttpServer() throws IOException {

        //创建socketChannel
        ServerSocketChannel channel = ServerSocketChannel.open();
        //创建serverSocket
        ServerSocket socket = channel.socket();
        //绑定端口号
        socket.bind(new InetSocketAddress("127.0.0.1",4977));
        //开启非阻塞
        channel.configureBlocking(false);
        //注册选择器
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);
//
//        //创建处理器
//        bookstudytest.nio.Handler handler = new bookstudytest.nio.Handler(1024);
        while (true) {
            if (selector.select(3000)==0){
                System.out.println("超时！");
                continue;
            }
            //获取待处理的SelectionKey
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey selectionKey = iterator.next();
                new Thread(new HttpHandler(selectionKey)).start();
                iterator.remove();
            }
        }
    }



    public void createServer() throws IOException {
        //创建socketChannel
        ServerSocketChannel channel = ServerSocketChannel.open();
        //创建serverSocket
        ServerSocket socket = channel.socket();
        //绑定端口号
        socket.bind(new InetSocketAddress("127.0.0.1",4977));
        //开启非阻塞
        channel.configureBlocking(false);
        //注册选择器
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);

        //创建处理器
        bookstudytest.nio.Handler handler = new bookstudytest.nio.Handler(1024);
        while (true){
            //
            if (selector.select(3000)==0){
                System.out.println("超时！");
                continue;
            }
            System.out.println("处理请求");
            //获取待处理的SelectionKey
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey selectionKey = iterator.next();
                try {
                    //如果是接收连接请求
                    if (selectionKey.isAcceptable()){
                        handler.handlerAccept(selectionKey);
                    }
                    //如果读数据请求
                    if (selectionKey.isReadable()){
                        handler.handleRead(selectionKey);
                    }
                } catch (IOException io){
                    iterator.remove();
                    continue;
                }

            }
        }

    }
}
